﻿using System;
using System.Collections.Generic;
using System.Data;

namespace Nvigorate.Data
{
    /// <summary>
    /// Factory for creating <see cref="IDatabaseCommand"/>.
    /// </summary>
    public interface IDatabaseCommandFactory
    {
        IDatabaseCommand GetCommand(string commandText, IDatabaseProvider provider);

        IDatabaseCommand GetCommand(string commandText, IDatabaseProvider provider,
                                    int timeout, IEnumerable<IDbDataParameter> parameters);

        IDatabaseCommand GetCommand(string commandText, IDatabaseProvider provider, int timeout,
                                                    DataTable parameters);

        IDatabaseCommand GetCommand(IDbCommand command, IDatabaseProvider provider);

        IDatabaseCommand GetCommand(IDbCommand command, IDatabaseProvider provider,
                                                    IEnumerable<IDbDataParameter> parameters);

        IDatabaseCommand GetCommand(string commandText, IDatabaseProvider provider, int timeout,
                                                    DataRow[] rows);
    }

    /// <summary>
    /// Standard implementation of <see cref="IDatabaseCommandFactory"/>.
    /// </summary>
    internal class DatabaseCommandFactory : IDatabaseCommandFactory
    {
        public IDatabaseCommand GetCommand(string commandText, IDatabaseProvider provider)
        {
            return new DatabaseCommand(commandText, provider);
        }

        public IDatabaseCommand GetCommand(IDbCommand command, IDatabaseProvider provider)
        {
            return new DatabaseCommand(command, provider);
        }

        public IDatabaseCommand GetCommand(IDbCommand command, IDatabaseProvider provider,
            IEnumerable<IDbDataParameter> parameters)
        {
            return new DatabaseCommand(command, provider, parameters);
        }

        public IDatabaseCommand GetCommand(string commandText, IDatabaseProvider provider, int timeout,
                                           IEnumerable<IDbDataParameter> parameters)
        {
            return new Procedure(commandText, provider, timeout, parameters);
        }

        public IDatabaseCommand GetCommand(string commandText, IDatabaseProvider provider, int timeout,
            DataTable parameters)
        {
            return new Procedure(commandText, provider, timeout, parameters);
        }

        public IDatabaseCommand GetCommand(string commandText, IDatabaseProvider provider, int timeout,
            DataRow[] rows)
        {
            return new Procedure(commandText, provider, timeout, rows);
        }
    }
}